<?php

function listClasses()
{
    $data = T("manager_classes")->fetch_all_classes();
    if (! $data)
        return array();
    $back = array();
    foreach ($data as $v) {
        $back[$v["classid"]] = $v;
    }
    return $back;
}

function listStudent($classid)
{
    $data = T("manager_linked")->fetch_students_by_classid($classid);
    if (! $data)
        return array();
    $back = array();
    foreach ($data as $v) {
        $v["card"] = getCardInfo($v["uid"]);
        $back[$v["uid"]] = $v;
    }
    return $back;
}

function listTeachers($classid)
{
    $data = T("manager_linked")->fetch_teachers_by_classid($classid);
    if (! $data)
        return array();
    $back = array();
    foreach ($data as $v) {
        $v["card"] = getCardInfo($v["uid"]);
        $back[$v["uid"]] = $v;
    }
    return $back;
}

function addClass($classid, $displayname, $description, $master, $admission_year)
{
    return T("manager_classes")->insert(array(
        "classid" => $classid,
        "displayname" => $displayname,
        "description" => $description,
        "master" => $master,
        "admission_year" => $admission_year
    ), array(
        "classid",
        "admission_year"
    ));
}

function removeClass($classid)
{
    return T("manager_classes")->remove("`classid`=$classid");
}

function addSubject($name)
{
    return T("manager_subject")->insert(array(
        "displayname" => $name
    ));
}

/**
 * 设置班主任
 * 理论上一个班级只有一个班主任，一个班主任也只能带一个班，uid已经作为一个班的班主任，则返回false，设置无效。
 * 如需改变地位，请先将原班级master设置为-1或其他，即可取消uid的班主任状态
 *
 * @param -1 $classid
 *            班级id
 * @param -1 $uid
 *            班主任id
 * @return boolean|boolean|mysqli_result
 */
function setMaster($classid, $uid)
{
    if (get_master_class($uid)) {
        return false;
    }
    return T("manager_classes")->update(array(
        "master" => $uid
    ), "`classid`=$classid", array(
        "master"
    ));
}

function addStudent($uid, $classid, $stuid = -1)
{
    if ($stuid == - 1)
        $stuid = $uid;
    return T("manager_student")->insert(array(
        "uid" => $uid,
        "classid" => $classid,
        "stuid" => $stuid
    ), array(
        "uid",
        "classid",
        "stuid"
    ));
}

function getClass($classid)
{
    return T("manager_classes")->fetch_by_classid($classid);
}

function getClassByStudentUid($uid)
{
    return T("manager_linked")->fetch_class_by_student($uid);
}

function removeStudent($uid)
{
    return T("manager_student")->remove("`uid`=$uid");
}

function addTeacher($uid, $sid)
{
    return T("manager_teacher")->insert(array(
        "uid" => $uid,
        "sid" => $sid
    ), array(
        "uid",
        "sid"
    ));
}

function addTeacherToClass($uid, $classid)
{
    return T("manager_teacher_class")->insert(array(
        "uid" => $uid,
        "classid" => $classid
    ), array(
        "uid",
        "classid"
    ));
}

function removeTeacherToClass($uid, $classid)
{
    return T("manager_teacher_class")->remove("`uid`=$uid AND `classid`=$classid");
}

function removeTeacher($uid)
{
    T("manager_teacher_class")->remove("`uid`=$uid");
    return T("manager_teacher")->remove("`uid`=$uid");
}

function is_Student($uid)
{
    return T("manager_student")->count_something("`uid`=$uid") != 0;
}

function is_Student_Parent($uid)
{
    return T("manager_linked")->count_child($uid) != 0;
}

function is_Master($uid, $teacherid)
{
    return T("manager_linked")->fetch_master_by_uid_and_master($uid, $teacherid) != 0;
}

function is_Teacher($uid)
{
    return T("manager_teacher")->count_something("`uid`=$uid");
}

function getTeacherSubject($uid, $isChinese = true)
{
    $back = T("manager_linked")->fetch_teacher_subject($uid);
    if ($isChinese) {
        return $back["displayname"];
    } else {
        return $back;
    }
}

function getTeacherClasses($uid)
{
    $data = T("manager_linked")->fetch_classes_by_uid($uid);
    if (! $data)
        return array();
    $back = array();
    foreach ($data as $v) {
        $back[$v["classid"]] = $v;
    }
    return $back;
}

function getMasterClass($uid)
{
    return T("manager_classes")->fetch_classid_by_master($uid);
}

function getChildrenFromParent($uid)
{
    $data = T("manager_linked")->fetch_classes_by_parent_uid($uid);
    if (! $data)
        return array();
    $back = array();
    foreach ($data as $v) {
        $v["card"] = getCardInfo($v["childid"]);
        $back[$v["uid"]] = $v;
    }
    return $back;
}

function getClassdata($uid)
{
    $back = array();
    if (is_Student($uid)) {
        $back["isStudent"] = true;
        $back["class"] = getClassByStudentUid($uid);
        $back["isTeacher"] = false;
        $back["isParent"] = false;
        return $back;
    }
    $back["isStudent"] = false;
    if (is_Teacher($uid)) {
        $masterclasses = getMasterClass($uid);
        if ($masterclasses) {
            $back["isMaster"] = true;
            $back["masterClass"] = $masterclasses["classid"];
        } else {
            $back["isMaster"] = false;
        }
        $back["classes"] = getTeacherClasses($uid);
        $back["isTeacher"] = true;
        $back["subject"] = getTeacherSubject($uid);
    } else {
        $back["isTeacher"] = false;
    }
    $children = getChildrenFromParent($uid);
    if ($children) {
        $back["isParent"] = true;
        $back["children"] = $children;
    } else {
        $back["isParent"] = false;
    }
    return $back;
}

function apply_class($uid, $note, $classid)
{
    T("manager_class_apply")->insert(array(
        "uid" => $uid,
        "note" => $note,
        "classid" => $classid
    ), array(
        "classid",
        "uid"
    ));
}

function getApplyData($uid)
{
    $data = T("manager_class_apply")->fetch_all_by_uid($uid);
    if (! $data) {
        return array();
    }
    $data["classdata"] = getClass($data["classid"]);
    $data["master"] = getCardInfo($data["classdata"]["master"]);
    return $data;
}

function removeApplyData($uid)
{
    return T("manager_class_apply")->remove("`uid`=$uid");
}

function approveClassData($uid, $admin, $note)
{
    $data = getApplyData($uid);
    if (! $data) {
        return;
    }
    $class = getClass($data["classid"]);
    $classname = $class["displayname"];
    addStudent($data["uid"], $data["classid"], $data["uid"]);
    removeApplyData($uid);
    sendNotice($uid, "您的班级申请已通过，欢迎加入$classname,处理人：{sender} 理由：$note", $admin);
}

function rejectClassData($uid, $admin, $note)
{
    $data = getApplyData($uid);
    if (! $data) {
        return;
    }
    removeApplyData($uid);
    sendNotice($uid, "您的班级申请已被拒绝：$note ,处理人：{sender}", $admin);
}

function updateClassData($classid, $displayname, $introduce, $master, $admission_year, $enable)
{
    return T("manager_classes")->update(array(
        "displayname" => $displayname,
        "description" => $introduce,
        "master" => $master,
        "admission_year" => $admission_year,
        "enable" => $enable
    ), "`classid`=$classid", array(
        "master",
        "admission_year",
        "enable"
    ));
}

function getTeachersByClassid($classid)
{
    $data = T("manager_linked")->fetch_teachers_by_classid($classid);
    if (! $data) {
        return array();
    }
    $back = array();
    foreach ($data as $v) {
        $v["card"] = getCardInfo($v["uid"]);
        $back[$v["uid"]] = $v;
    }
    return $back;
}

function containSubject($classid, $sid)
{
    $data = T("manager_linked")->fetch_sid_by_classid($classid);
    if (! $data)
        return false;
    foreach ($data as $v) {
        if ($v["sid"] == $sid)
            return true;
    }
    return false;
}

function getEnabledClassList()
{
    $classes = T("manager_classes")->fetch_enabled_classes();
    if (! $classes) {
        return array();
    }
    $back = array();
    foreach ($classes as $v) {
        $back[$v["admission_year"]][$v["classid"]] = $v;
    }
    return $back;
}

function getApplyList($classid)
{
    $data = T("manager_class_apply")->fetch_all_by_classid($classid);
    $back = array();
    foreach ($data as $v) {
        $v["card"] = getCardInfo($v["uid"]);
        $v["classdata"] = getClass($v["classid"]);
        $back[$v["uid"]] = $v;
    }
    return $back;
}

function getStudents($classid)
{
    $data = T("manager_student")->fetch_all_by_classid($classid);
    if (! $data) {
        return array();
    }
    $back = array();
    foreach ($data as $v) {
        $v["card"] = getCardInfo($v["uid"]);
        $back[$v["uid"]] = $v;
    }
    return $back;
}

function getClassNum($classid)
{
    return T("manager_classes")->fetch_stu_num($classid);
}

function listAllClasses()
{
    $data = listClasses();
    $back = array();
    foreach ($data as $v) {
        $v["stunum"] = getClassNum($v["classid"]);
        $back[$v["admission_year"]][$v["classid"]] = $v;
    }
    return $back;
}

function getSubjectBySid($sid)
{
    return T("manager_subject")->fetch_assoc("`sid`=$sid", "*");
}
?>